package com.ctg.payroll2.service.workflow.imp;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ctg.payroll2.bean.filter.WorkflowTaskFilter;
import com.ctg.payroll2.bean.po.PayClass;
import com.ctg.payroll2.bean.po.workflow.FlowInstance;
import com.ctg.payroll2.bean.po.workflow.FlowMaster;
import com.ctg.payroll2.bean.po.workflow.FlowOrgchart;
import com.ctg.payroll2.bean.po.workflow.FlowPayData;
import com.ctg.payroll2.bean.po.workflow.FlowRuninfo;
import com.ctg.payroll2.bean.po.workflow.FlowRunnext;
import com.ctg.payroll2.bean.po.workflow.FlowStep;
import com.ctg.payroll2.bean.po.workflow.FlowStepRun;
import com.ctg.payroll2.dao.workflow.WorkflowTaskDao;
import com.ctg.payroll2.service.BaseService;
import com.ctg.payroll2.service.workflow.WorkflowTaskService;
import com.ctg.platform.util.Page;

public class WorkflowTaskServiceImp extends BaseService implements
		WorkflowTaskService {
	private WorkflowTaskDao dao;

	/**
	 * @param dao 设置dao
	 */
	public void setDao(WorkflowTaskDao dao) {
		this.dao = dao;
	}
	
	public Page queryTask(WorkflowTaskFilter filter){
		return dao.queryTask(filter);
	}
	
	public Integer querySubCompanyCount(WorkflowTaskFilter filter) throws SQLException{
		return dao.querySubCompanyCount(filter);
	}

	public FlowStepRun queryStartFlowStepRun(WorkflowTaskFilter filter) throws SQLException{
		return dao.queryStartFlowStepRun(filter);
	}
	
	public List querySubCompanys(WorkflowTaskFilter filter) throws SQLException{
		return dao.querySubordinateByCompanyCode(filter);
	}

	public int queryPayLogByPeriod(WorkflowTaskFilter filter) throws SQLException{
		return dao.queryPayLogByPeriod(filter);
	}
	
	public int getEmployeeCountOfCompany(WorkflowTaskFilter filter) throws SQLException{
		return dao.getEmployeeCountOfCompany(filter);
	}

	public int getEmployeeCountOfPayData(WorkflowTaskFilter filter) throws SQLException{
		return dao.getEmployeeCountOfPayData(filter);
	}
	
	public FlowInstance queryFlowInstance(WorkflowTaskFilter filter) throws SQLException{
		return dao.queryFlowInstance(filter);
	}
	
	public List<PayClass>  queryCompanyPayData(WorkflowTaskFilter filter) throws SQLException{
		return dao.queryPayClassIdFromPayData(filter);
	}
	
	public List<FlowPayData>  queryAreaFromPayData(WorkflowTaskFilter filter) throws SQLException{
		return dao.queryAreaFromPayData(filter);
	}

	public List<FlowPayData>  queryGroupFromPayData(WorkflowTaskFilter filter) throws SQLException{
		List company_codes=dao.querySubCompany(filter);
		List groupPayData=new ArrayList();
		for(int i=0;i<company_codes.size();i++){
			FlowPayData temp=(FlowPayData)company_codes.get(i);
			filter.setCompany_code(temp.getCompany_code());
			groupPayData.addAll(dao.queryGroupFromPayData(filter));
		}
		return groupPayData;
	}
	
	public void addTask(WorkflowTaskFilter filter) throws SQLException{
		FlowMaster flowMaster=dao.queryFlowMaster(filter);
		FlowStep flowStep=dao.queryStartFlowStep(filter);
		FlowStepRun flowStepRun=dao.queryStartFlowStepRun(filter);
		
		FlowInstance flowInstance=new FlowInstance();
		flowInstance.setFlow_id(flowMaster.getFlow_id());
		flowInstance.setFlow_status("A");
		flowInstance.setPriod(filter.getPriod());
		flowInstance.setCompany_code(filter.getCompany_code());
		flowInstance.setFlow_at("1");
		//flowInstance.setThis_company(this_company);
		//flowInstance.setPre_insid(pre_insid);
		
		Long flowinsid=dao.insertFlowInstance(flowInstance);
		
		FlowRunnext flowRunnext=new FlowRunnext();
		flowRunnext.setFlow_id(flowMaster.getFlow_id());
		flowRunnext.setFlow_insid(flowinsid);
		flowRunnext.setStep_id(flowStep.getStep_id());
		flowRunnext.setFlow_status("E");
		flowRunnext.setStep_runtype(flowStepRun.getRun_type().toString());
		if(flowStepRun.getRun_type().equals(0)){
			flowRunnext.setStep_role(flowStepRun.getRun_user());
		}else{
			flowRunnext.setStep_user(flowStepRun.getRun_user());
		}
		flowRunnext.setStep_user(filter.getAccount_id());
		flowRunnext.setStep_type(flowStep.getStep_type());
		dao.insertFlowRunnext(flowRunnext);
		
		FlowRuninfo flowRuninfo=new FlowRuninfo();
		flowRuninfo.setFlow_id(flowMaster.getFlow_id().toString());
		flowRuninfo.setFlow_insid(flowinsid.toString());
		flowRuninfo.setStep_id(flowStep.getStep_id().toString());
		flowRuninfo.setStep_role(flowRunnext.getStep_role());
		flowRuninfo.setStep_user(flowRunnext.getStep_user());
		flowRuninfo.setFlow_action("0");
		if(filter.getFlow_memo().equals("")){
			filter.setFlow_memo("同意");
		}
		flowRuninfo.setFlow_memo(filter.getFlow_memo());
		dao.insertFlowRuninfo(flowRuninfo);
		
		filter.setStep_id(flowStep.getStep_id().toString());
		FlowStep nextflowStep=dao.queryNextFlowStep(filter);
		FlowStepRun nextflowStepRun=dao.queryFlowStepRun(filter);
		FlowRunnext nextflowRunnext=new FlowRunnext();
		nextflowRunnext.setFlow_id(nextflowStep.getFlow_id());
		nextflowRunnext.setFlow_insid(flowinsid);
		nextflowRunnext.setStep_id(nextflowStep.getStep_id());
		nextflowRunnext.setFlow_status("A");
		nextflowRunnext.setStep_runtype(nextflowStepRun.getRun_type().toString());
		if(nextflowStepRun.getRun_type().equals(0)){
			nextflowRunnext.setStep_role(nextflowStepRun.getRun_user());
		}else{
			nextflowRunnext.setStep_user(nextflowStepRun.getRun_user());
		}
		nextflowRunnext.setStep_type(nextflowStep.getStep_type());
		dao.insertFlowRunnext(nextflowRunnext);
		dao.updatePayData(filter);
	}
	
	public void updateTask(WorkflowTaskFilter filter) throws SQLException{
		//分公司当前步骤结束
		filter.setStep_type(null);
		FlowRunnext flowRunnext=dao.queryFlowRunnext(filter);
		flowRunnext.setFlow_status("E");
		flowRunnext.setStep_user(filter.getAccount_id());
		dao.updateFlowRunnext(flowRunnext);
		//分公司当前步骤记录
		FlowRuninfo flowRuninfo=new FlowRuninfo();
		flowRuninfo.setFlow_id(filter.getFlow_id());
		flowRuninfo.setFlow_insid(filter.getFlow_insid());
		flowRuninfo.setStep_id(filter.getStep_id());
		flowRuninfo.setStep_role(flowRunnext.getStep_role());
		flowRuninfo.setStep_user(flowRunnext.getStep_user());
		flowRuninfo.setFlow_action("0");
		if(filter.getFlow_memo().equals("")){
			filter.setFlow_memo("同意");
		}
		flowRuninfo.setFlow_memo(filter.getFlow_memo());
		flowRuninfo.setStart_date(flowRunnext.getStart_date());
		dao.insertFlowRuninfo(flowRuninfo);
		filter.setStatus("2");
		dao.updatePayData(filter);
		//是否最后一步
		if(flowRunnext.getStep_type().equals("E")){
			//是最后一步，分公司instance结束
			FlowInstance flowInstance=dao.queryFlowInstance(filter);
			flowInstance.setFlow_status("E");
			dao.updateFlowInstance(flowInstance);
			//获得上级信息
			FlowOrgchart flowOrgchart=dao.queryParentFlowOrgchat(filter);
			if(flowOrgchart==null){
				
			}else{
			//是否需要等待所有下级
				if(flowOrgchart.getIs_waitall().equals("1")){//是
					WorkflowTaskFilter pfilter=new WorkflowTaskFilter();
					pfilter.setCompany_code(flowOrgchart.getCompany_code());
					Integer subCompanycount=dao.querySubCompanyCount(pfilter);//子公司个数
					Integer subInstancecount=dao.querySubInstance_count(filter);//结束的子instance个数
					//是否所有下级instance完成
					if(subCompanycount==subInstancecount){//是
						FlowMaster pflowMaster=dao.queryFlowMaster(pfilter);
						FlowStep pflowStep=dao.queryStartFlowStep(pfilter);
						FlowStepRun pflowStepRun=dao.queryStartFlowStepRun(pfilter);
						//新建上级instance
						FlowInstance pflowInstance=new FlowInstance();
						pflowInstance.setFlow_id(pflowMaster.getFlow_id());
						pflowInstance.setFlow_status("A");
						pflowInstance.setPriod(filter.getPriod());
						pflowInstance.setCompany_code(pfilter.getCompany_code());
						pflowInstance.setFlow_at("2");
						//pflowInstance.setThis_company(filter.getCompany_code());
						pflowInstance.setPre_insid(filter.getFlow_insid());
						
						Long pflowinsid=dao.insertFlowInstance(pflowInstance);
						//新建开始步骤
						FlowRunnext pflowRunnext=new FlowRunnext();
						pflowRunnext.setFlow_id(pflowMaster.getFlow_id());
						pflowRunnext.setFlow_insid(pflowinsid);
						pflowRunnext.setStep_id(pflowStep.getStep_id());
						pflowRunnext.setFlow_status("A");
						pflowRunnext.setStep_runtype(pflowStepRun.getRun_type().toString());
						if(pflowStepRun.getRun_type().equals(0)){
							flowRunnext.setStep_role(pflowStepRun.getRun_user());
						}else{
							flowRunnext.setStep_user(pflowStepRun.getRun_user());
						}
						pflowRunnext.setStep_user(filter.getAccount_id());
						pflowRunnext.setStep_type(pflowStep.getStep_type());
						dao.insertFlowRunnext(pflowRunnext);
					}else{//否
						
					}
				}else{//否，新建上级instance和开始步骤
					WorkflowTaskFilter pfilter=new WorkflowTaskFilter();
					pfilter.setCompany_code(flowOrgchart.getCompany_code());
					FlowMaster pflowMaster=dao.queryFlowMaster(pfilter);
					FlowStep pflowStep=dao.queryStartFlowStep(pfilter);
					FlowStepRun pflowStepRun=dao.queryStartFlowStepRun(pfilter);
					//新建上级instance
					FlowInstance pflowInstance=new FlowInstance();
					pflowInstance.setFlow_id(pflowMaster.getFlow_id());
					pflowInstance.setFlow_status("A");
					pflowInstance.setPriod(filter.getPriod());
					pflowInstance.setCompany_code(pfilter.getCompany_code());
					pflowInstance.setFlow_at("2");
					pflowInstance.setThis_company(filter.getCompany_code());
					pflowInstance.setPre_insid(filter.getFlow_insid());
					
					Long pflowinsid=dao.insertFlowInstance(pflowInstance);
					//新建开始步骤
					FlowRunnext pflowRunnext=new FlowRunnext();
					pflowRunnext.setFlow_id(pflowMaster.getFlow_id());
					pflowRunnext.setFlow_insid(pflowinsid);
					pflowRunnext.setStep_id(pflowStep.getStep_id());
					pflowRunnext.setFlow_status("A");
					pflowRunnext.setStep_runtype(pflowStepRun.getRun_type().toString());
					if(pflowStepRun.getRun_type().equals(0)){
						flowRunnext.setStep_role(pflowStepRun.getRun_user());
					}else{
						flowRunnext.setStep_user(pflowStepRun.getRun_user());
					}
					pflowRunnext.setStep_user(filter.getAccount_id());
					pflowRunnext.setStep_type(pflowStep.getStep_type());
					dao.insertFlowRunnext(pflowRunnext);
				}
			}
		}else{//不是最后一步，继续新建下一步骤
			FlowStep nextflowStep=dao.queryNextFlowStep(filter);
			FlowStepRun nextflowStepRun=dao.queryFlowStepRun(filter);
			filter.setStep_id(nextflowStep.getStep_id().toString());
			FlowRunnext nextflowRunnext=dao.queryFlowRunnext(filter);
			if(nextflowRunnext==null){
				nextflowRunnext=new FlowRunnext();
				nextflowRunnext.setFlow_id(Long.parseLong(filter.getFlow_id()));
				nextflowRunnext.setFlow_insid(Long.parseLong(filter.getFlow_insid()));
				nextflowRunnext.setStep_id(nextflowStep.getStep_id());
				nextflowRunnext.setFlow_status("A");
				nextflowRunnext.setStep_runtype(nextflowStepRun.getRun_type().toString());
				if(nextflowStepRun.getRun_type().equals(0)){
					nextflowRunnext.setStep_role(nextflowStepRun.getRun_user());
				}else{
					nextflowRunnext.setStep_user(nextflowStepRun.getRun_user());
				}
				nextflowRunnext.setStep_type(nextflowStep.getStep_type());
				dao.insertFlowRunnext(nextflowRunnext);
			}else{

				nextflowRunnext.setFlow_id(Long.parseLong(filter.getFlow_id()));
				nextflowRunnext.setFlow_insid(Long.parseLong(filter.getFlow_insid()));
				nextflowRunnext.setStep_id(nextflowStep.getStep_id());
				nextflowRunnext.setFlow_status("A");
				nextflowRunnext.setStep_runtype(nextflowStepRun.getRun_type().toString());
				if(nextflowStepRun.getRun_type().equals(0)){
					nextflowRunnext.setStep_role(nextflowStepRun.getRun_user());
				}else{
					nextflowRunnext.setStep_user(nextflowStepRun.getRun_user());
				}
				nextflowRunnext.setStep_type(nextflowStep.getStep_type());
				dao.updateFlowRunnext(nextflowRunnext);
			}
		}
	}
	
	public void rollBackTask(WorkflowTaskFilter filter) throws SQLException{
		//是最后一步，分公司instance退回
		FlowInstance flowInstance=dao.queryFlowInstance(filter);
		flowInstance.setFlow_status("R");
		dao.updateFlowInstance(flowInstance);
//		分公司当前步骤结束
		FlowRunnext flowRunnext=dao.queryFlowRunnext(filter);
		flowRunnext.setFlow_status("R");
		flowRunnext.setStep_user(filter.getAccount_id());
		dao.updateFlowRunnext(flowRunnext);
		//分公司当前步骤记录
		FlowRuninfo flowRuninfo=new FlowRuninfo();
		flowRuninfo.setFlow_id(filter.getFlow_id());
		flowRuninfo.setFlow_insid(filter.getFlow_insid());
		flowRuninfo.setStep_id(filter.getStep_id());
		flowRuninfo.setStep_role(flowRunnext.getStep_role());
		flowRuninfo.setStep_user(flowRunnext.getStep_user());
		flowRuninfo.setFlow_action("1");
		if(filter.getFlow_memo().equals("")){
			filter.setFlow_memo("退回");
		}
		flowRuninfo.setFlow_memo(filter.getFlow_memo());
		flowRuninfo.setStart_date(flowRunnext.getStart_date());
		dao.insertFlowRuninfo(flowRuninfo);
		if(flowRunnext.getStep_type().equals("S")){
			if(flowInstance.getThis_company().equals("")){
				String[] rollcompany_codes=filter.getRollcompany_code().split(",");
				for(int i=0;i<rollcompany_codes.length;i++){
					WorkflowTaskFilter pfilter=new WorkflowTaskFilter();
					pfilter.setFlow_insid(rollcompany_codes[i]);
					FlowInstance pflowInstance=dao.queryFlowInstance(pfilter);
					pflowInstance.setFlow_status("R");
					dao.updateFlowInstance(pflowInstance);
					pfilter.setStep_type("S");
					FlowRunnext pflowRunnext=dao.queryFlowRunnext(pfilter);
					pflowRunnext.setFlow_status("A");
					dao.updateFlowRunnext(pflowRunnext);
					filter.setStatus("1");
					filter.setCompany_code(flowInstance.getThis_company());
					dao.updatePayData(filter);
				}
			}else{
				WorkflowTaskFilter pfilter=new WorkflowTaskFilter();
				pfilter.setFlow_insid(flowInstance.getPre_insid());
				FlowInstance pflowInstance=dao.queryFlowInstance(pfilter);
				pflowInstance.setFlow_status("R");
				dao.updateFlowInstance(pflowInstance);
				pfilter.setStep_type("S");
				FlowRunnext pflowRunnext=dao.queryFlowRunnext(pfilter);
				pflowRunnext.setFlow_status("A");
				dao.updateFlowRunnext(pflowRunnext);
				filter.setStatus("1");
				filter.setCompany_code(flowInstance.getThis_company());
				dao.updatePayData(filter);
			}
		}else{
			FlowRunnext pflowRunnext=dao.queryParentRunnext(filter);
			pflowRunnext.setFlow_status("A");
			dao.updateFlowRunnext(pflowRunnext);
		}
	}
}
